package com.imooc.controller;

import com.imooc.pojo.Stu;
import com.imooc.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

//@Controller
@RestController // 默认返回json对象
public class StuController {
    @Autowired
    private StuService stuService;

    @GetMapping("/getStu")
    public Object hello(int id) {
        return stuService.getStu(id);
    }

    public void saveParent() {
        Stu stu = new Stu();
        stu.setName("parent");
        stu.setAge(23);
        stuService.save(stu);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void save() {
        saveParent();
        try {
            stuService.saveChild();
        }catch (Exception ex){

        }

    }

}
